package com.yinhai.message.manage.wxregister.dao;

import java.util.ArrayList;
import java.util.List;

import com.yinhai.core.common.api.util.ValidateUtil;
import com.yinhai.core.service.ta3.domain.dao.HibernateDAO;
import com.yinhai.message.manage.wxregister.po.WxConfigPo;
import com.yinhai.message.manage.wxregister.vo.WxConfigVo;

public class WxConfigDaoImpl extends HibernateDAO<WxConfigPo> implements IWxConfigDao{

	@Override
	public List<WxConfigVo> queryWxList(String appid, String name) {
		List<Object> values= new ArrayList<Object>();
		StringBuilder hql = new StringBuilder();
        hql.append("select new com.yinhai.message.manage.wxregister.vo.WxConfigVo(")
        .append(" id,")
        .append(" name,")
        .append(" appid,")
        .append(" secret,")
        .append(" defaultwx)")
        .append(" from WxConfigPo ")
        .append(" where 1=1 ");
        if(ValidateUtil.isNotEmpty(appid)){
        	hql.append(" and appid like ? ");
        	appid = "%"+appid+"%";
        	values.add(appid);
        }
        if(ValidateUtil.isNotEmpty(name)){
        	hql.append(" and name = ? ");
        	values.add(name);
        }
        return super.selectFromMultiTable(hql.toString(),values.toArray());
	}

	@Override
	public void saveWx(WxConfigPo po) {
		super.save(po);
	}

	@Override
	public void updateWx(WxConfigPo po) {
		super.update(po);
	}

	@Override
	public void removeWx(Long id) {
		StringBuilder hql = new StringBuilder();
		hql.append("delete WxConfigPo where id = ? ");
		super.delete(hql.toString(), id);
	}

	@Override
	public void removeDefault() {
		StringBuilder hql = new StringBuilder();
		 hql.append("update WxConfigPo set defaultwx = '0' ");
		 super.update(hql.toString());
	}

	@Override
	public WxConfigPo getDefaultWx() {
		StringBuilder hql = new StringBuilder();
		hql.append("from WxConfigPo where defaultwx = '1' ");
		return super.get(hql.toString());
	}

}
